package com.csii.mobilebanking;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.csii.mobilebanking.Strings.Extra;
import com.csii.mobilebanking.tool.HpGlobals;
import com.csii.mobilebanking.user.LoginActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class Main extends BaseActivity implements OnClickListener{
	
	private ViewPager viewPager;
	private static final String STATE_POSITION = "STATE_POSITION";
	TextView tv_account;
	Button b_reg, b_login;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout);
		loadView();
		DisplayImageOptions options = new DisplayImageOptions.Builder()
			.showImageForEmptyUri(R.drawable.ic_empty)
			.showImageOnFail(R.drawable.ic_error)
			.resetViewBeforeLoading()
			.cacheOnDisc()
			.imageScaleType(ImageScaleType.EXACTLY)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.displayer(new FadeInBitmapDisplayer(300))
			.build();
		viewPager.setAdapter(new ImagePagerAdapter(this,options,Strings.IMAGE_URLS));
		int pagerPosition = 0;

		if (savedInstanceState != null) {
			pagerPosition = savedInstanceState.getInt(STATE_POSITION);
		}
		viewPager.setCurrentItem(pagerPosition);

	}

	private void loadView() {
		b_reg = (Button) findViewById(R.id.btn_regisiter);
		b_login = (Button) findViewById(R.id.btn_login);
		((Button)findViewById(R.id.btn_login)).setOnClickListener(this);
		((Button)findViewById(R.id.btn_regisiter)).setOnClickListener(this);
		((TextView)findViewById(R.id.life_more)).setOnClickListener(this);
		((TextView)findViewById(R.id.announcement_more)).setOnClickListener(this);
		((LinearLayout)findViewById(R.id.main_life0)).setOnClickListener(this);
		((LinearLayout)findViewById(R.id.main_life1)).setOnClickListener(this);
		((LinearLayout)findViewById(R.id.main_life2)).setOnClickListener(this);
		((TextView)findViewById(R.id.main_ann_text0)).setOnClickListener(this);
		((TextView)findViewById(R.id.main_ann_text1)).setOnClickListener(this);
		((TextView)findViewById(R.id.main_ann_text2)).setOnClickListener(this);
		((TextView)findViewById(R.id.main_ann_text3)).setOnClickListener(this);
		((LinearLayout)findViewById(R.id.tool_btn0)).setOnClickListener(this);
		((LinearLayout)findViewById(R.id.tool_btn1)).setOnClickListener(this);
		((LinearLayout)findViewById(R.id.tool_btn2)).setOnClickListener(this);
		((TextView)findViewById(R.id.main_present)).setOnClickListener(this);
		((TextView)findViewById(R.id.help_number)).setOnClickListener(this);
		((ImageView)findViewById(R.id.main_debit_card)).setOnClickListener(this);
		((LinearLayout)findViewById(R.id.main_lin_card0)).setOnClickListener(this);
		((LinearLayout)findViewById(R.id.main_lin_card1)).setOnClickListener(this);
		((LinearLayout)findViewById(R.id.main_lin_card2)).setOnClickListener(this);
		viewPager=(ViewPager)findViewById(R.id.img_pager);
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putInt(STATE_POSITION, viewPager.getCurrentItem());
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	@Override
	protected void onResume() {
		super.onResume();
		if (HpGlobals.mXGUserAccount != null) {
			b_reg.setVisibility(View.GONE);
			b_login.setVisibility(View.GONE);
			tv_account.setVisibility(View.VISIBLE);
			tv_account.setText(HpGlobals.mXGUserAccount.RealName);
		}
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_login:
			startActivity(new Intent(this,LoginActivity.class));
			break;
		case R.id.btn_regisiter:
			tomstMessage(R.string.not_open);
			break;
		case R.id.life_more:
			startActivity(new Intent(this,FrameContent.class)
				.putExtra(Extra.LSIT_TITLE_KEY, R.string.frame_title0)
				.putExtra(Extra.MORE_TITLE_KEY, Strings.MORE_MSG_TITLE)
				.putExtra(Extra.MORE_CONTENT_KEY, Strings.MORE_MSG_CONTENT)
				.putExtra(Extra.MORE_DATE_KEY, Strings.MORE_MSG_DATE));
			break;
		case R.id.main_life0:
			frameContentInd0(0);
			break;
		case R.id.main_life1:
			frameContentInd0(1);
			break;
		case R.id.main_life2:
			frameContentInd0(2);
			break;
		case R.id.announcement_more:
			startActivity(new Intent(this,FrameContent.class)
				.putExtra(Extra.LSIT_TITLE_KEY, R.string.frame_title1)
				.putExtra(Extra.MORE_TITLE_KEY, Strings.MORE_ANN_TITLE)
				.putExtra(Extra.MORE_CONTENT_KEY, Strings.MORE_ANN_CONTENT)
				.putExtra(Extra.MORE_DATE_KEY, Strings.MORE_ANN_DATE));
			break;
		case R.id.main_ann_text0:
			frameContentInd1(0);
			break;
		case R.id.main_ann_text1:
			frameContentInd1(1);
			break;
		case R.id.main_ann_text2:
			frameContentInd1(2);
			break;
		case R.id.main_ann_text3:
			frameContentInd1(3);
			break;
		case R.id.tool_btn0:
			tomstMessage(R.string.not_open);
			break;
		case R.id.tool_btn1:
			tomstMessage(R.string.not_open);
			break;
		case R.id.tool_btn2:
			tomstMessage(R.string.not_open);
			break;
		case R.id.main_present:
			tomstMessage(R.string.not_open);
			break;
		case R.id.help_number:
			startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+"400899999")));
			break;
		case R.id.main_debit_card:
			startActivity(new Intent(this,LoadURLWebView.class)
				.putExtra(Extra.WEB_VIEW_TITLE, "移动金融借记卡在线申请")
				.putExtra(Extra.WEB_VIEW_URL, "https://apply.95559.com.cn/netsign/apply/ns0001_debitcard_apply_req.jsp"));
			break;
		case R.id.main_lin_card0:
			tomstMessage(R.string.not_open);
			break;
		case R.id.main_lin_card1:
			tomstMessage(R.string.not_open);
			break;
		case R.id.main_lin_card2:
			tomstMessage(R.string.not_open);
			break;
		}
		
	}
	
	private void frameContentInd0(int indext){
		startActivity(new Intent(this,FrameContent.class)
		.putExtra(Extra.LSIT_TITLE_KEY, R.string.frame_title0)
		.putExtra(Extra.MORE_TITLE_KEY, Strings.MORE_MSG_TITLE[indext])
		.putExtra(Extra.MORE_CONTENT_KEY, Strings.MORE_MSG_CONTENT[indext])
		.putExtra(Extra.MORE_DATE_KEY, Strings.MORE_MSG_DATE[indext]));
	}
	
	private void frameContentInd1(int indext){
		startActivity(new Intent(this,FrameContent.class)
		.putExtra(Extra.LSIT_TITLE_KEY, R.string.frame_title1)
		.putExtra(Extra.MORE_TITLE_KEY, Strings.MORE_ANN_TITLE[indext])
		.putExtra(Extra.MORE_CONTENT_KEY, Strings.MORE_ANN_CONTENT[indext])
		.putExtra(Extra.MORE_DATE_KEY, Strings.MORE_ANN_DATE[indext]));
	}
	
	private void tomstMessage(int stringId){
		Toast.makeText(this, stringId, Toast.LENGTH_SHORT).show();
	}


	@Override
	public void onBackPressed() {
		finish();
	}

}
